package com.pb.wkflow.api.controller.assembler;

import com.pb.infra.oth.util.UserInfo;
import com.pb.wkflow.api.controller.assembler.mapper.ProcessTaskDtoMapper;
import com.pb.wkflow.api.controller.request.ProcessTaskRequest;
import com.pb.wkflow.api.query.CompleteTaskQuery;
import com.pb.wkflow.api.query.MyApplyTaskQuery;
import com.pb.wkflow.api.query.ProcInstanceQuery;
import com.pb.wkflow.api.query.TodoTaskQuery;
import com.pb.wkflow.core.camunda.bpmn.instance.Participants;
import com.pb.wkflow.core.entity.PostInfo;
import com.pb.wkflow.core.utils.AssertUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ProcTaskAssembler
 * @author 苗益辉
 * @date 2022/9/20 15:57
 */
public class ProcTaskAssembler {

    private ProcTaskAssembler() {

    }

    private static List<PostInfo> getUserPostInfo(UserInfo userInfo) {
        List<PostInfo> postInfos = new ArrayList<>();

        PostInfo user = PostInfo.builder().build();
        user.setType(Participants.TYPE_USER);
        user.setUserId(userInfo.getUserId());
        postInfos.add(user);

        PostInfo org = PostInfo.builder().build();
        org.setType(Participants.TYPE_ORG);
        org.setOrgCode(userInfo.getOrgId());
        postInfos.add(org);

        PostInfo pos = PostInfo.builder().build();
        pos.setType(Participants.TYPE_POS);
        pos.setPosCode(userInfo.getPostId());
        postInfos.add(pos);

        PostInfo job = PostInfo.builder().build();
        job.setType(Participants.TYPE_JOB);
        job.setPosCode(userInfo.getPositionId());
        postInfos.add(job);

        PostInfo role = PostInfo.builder().build();
        role.setType(Participants.TYPE_ROLE);
        role.setRoleCodes(userInfo.getRoles());
        postInfos.add(role);

        PostInfo post = PostInfo.builder().build();
        post.setType(Participants.TYPE_POST);
        post.setOrgCode(userInfo.getOrgId());
        post.setPosCode(userInfo.getPostId());
        post.setJobCode(userInfo.getPositionId());
        post.setRoleCodes(userInfo.getRoles());
        postInfos.add(post);

        return postInfos;
    }

    public static TodoTaskQuery createTodoTaskQuery(ProcessTaskRequest request, UserInfo userInfo) {
        TodoTaskQuery taskQuery = ProcessTaskDtoMapper.INSTANCE.assemblerToTodoQuery(request, userInfo);
        taskQuery.setPostInfos(getUserPostInfo(userInfo));
        taskQuery.createTimeDesc();
        return taskQuery;
    }

    public static CompleteTaskQuery createCompleteTaskQuery(ProcessTaskRequest request, UserInfo userInfo) {
        CompleteTaskQuery taskQuery = ProcessTaskDtoMapper.INSTANCE.assemblerToCompleteQuery(request, userInfo);
        taskQuery.startTimeDesc();
        return taskQuery;
    }

    public static MyApplyTaskQuery createMyApplyTaskQuery(ProcessTaskRequest request, UserInfo userInfo) {
        MyApplyTaskQuery taskQuery = ProcessTaskDtoMapper.INSTANCE.assemblerToMyApplyQuery(request, userInfo);
        taskQuery.startTimeDesc();
        return taskQuery;
    }

    public static ProcInstanceQuery createTodoProcInstanceQuery(ProcessTaskRequest request) {
        AssertUtils.notEmpty(request.getType(), "列表类型不能为空");
        ProcInstanceQuery query = ProcessTaskDtoMapper.INSTANCE.assemblerToQuery(request);
        if ("todo".equals(request.getType())) {
            query.startTimeDesc();
        } else if ("done".equals(request.getType())) {
            query.endTimeDesc();
        }
        return query;
    }
}
